<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      选择加注机下拉框-->
      <el-form-item label="加注机" prop="facilitId">
        <el-select v-model="queryParams.facilitId"   placeholder="请选择加注机" clearable>
          <el-option
            v-for="item in facilitList"
            :key="item.facilitId"
            :label="item.facilitName"
            :value="item.facilitId"
            @change="chooseFac(item.facilitId)"
          />
        </el-select>
      </el-form-item>
      <!--      选择公司下拉框-->
      <el-form-item label="公司名称" prop="companyId">
        <el-select v-model="queryParams.companyId"   placeholder="请选择加注机" clearable>
          <el-option
            v-for="item in companyList"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
            @change="chooseCompany(item.companyId)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="司机姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入司机用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车队经理" prop="facilitId">
        <el-select v-model="queryParams.mangerId"   placeholder="请选择车队经理" clearable>
          <el-option
            v-for="item in mangerList"
            :key="item.mangerId"
            :label="item.userName"
            :value="item.mangerId"
            @change="chooseManger(item.mangerId)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNum">
        <el-input
          v-model="queryParams.plateNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>


        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--      数据区域-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="5"><div class="grid-content bg-purple"><span>加注金额</span><h2>1500元</h2></div></el-col>
        <el-col :span="5"><div class="grid-content bg-purple"><span>加注金额</span><h2>1500元</h2></div></el-col>
        <el-col :span="5"><div class="grid-content bg-purple"><span>加注金额</span><h2>1500元</h2></div></el-col>
        <el-col :span="5"><div class="grid-content bg-purple"   style="align-content: center">
          <el-radio-group v-model="radio1">
            <el-radio-button label="今日"></el-radio-button>
            <el-radio-button label="本周"></el-radio-button>
            <el-radio-button label="本月"></el-radio-button>
          </el-radio-group>
        </div></el-col>
        <el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
      </el-row>
      <!--        <div class="box-card-mount">-->
      <!--          <span>加注金额</span>-->
      <!--          <h2>1500元</h2>-->
      <!--        </div>-->
      <!--        <div class="box-card-mount">-->
      <!--          <span>加注金额</span>-->
      <!--          <h2>1500元</h2>-->
      <!--        </div>-->


    </el-card>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['consumer:statistic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consumer:statistic:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statisticList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="statisticId" />
      <el-table-column label="加注金额(元)" align="center" prop="orderFilled" />
      <el-table-column label="支付方式" align="center" prop="statisticType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.statistic_order_type" :value="scope.row.statisticType"/>
        </template>
      </el-table-column>
      <el-table-column label="车属公司" align="center" prop="companyId" />
      <el-table-column label="车牌号" align="center" prop="plateNum" />
      <el-table-column label="加注单价(元)" align="center" prop="orderPrice" />
      <el-table-column label="加注体积(kg)" align="center" prop="orderFilled" />
      <el-table-column label="司机姓名" align="center" prop="userName" />
      <el-table-column label="车队经理" align="center" prop="mangerId" />
      <el-table-column label="设备名称" align="center" prop="facilitId" />
      <el-table-column label="加注位置" align="center" prop="userName" >
        <template slot-scope="scope">
          <el-button slot="append" icon="el-icon-location" @click="openMap(scope.row.facilitLat,scope.row.facilitLng)"></el-button>
          <MapSelect ref="ms"/>
        </template>
      </el-table-column>
      <el-table-column label="加注时间" align="center" prop="createTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consumer:statistic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listStatistic, getStatistic, delStatistic, addStatistic, updateStatistic } from "@/api/consumer/statistic";
import MapSelect from "@/views/components/MapSelect.vue";
import {listFacilit} from "@/api/consumer/facilit";
import {listCompany} from "@/api/consumer/company";

export default {
  name: "Statistic",
  components: {MapSelect},
  dicts: ['statistic_order_type'],
  data() {
    return {
      // 多选框选中数组
      radio1: '上海',
      radio2: '上海',
      radio3: '上海',
      radio4: '上海',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 统计总表格数据
      statisticList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 加注机列表数据
      facilitList: [],
      // 公司列表数据
      companyList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        facilitId: null,
        fillerId: null,
        userId: null,
        userName: null,
        companyId: null,
        mangerId: null,
        leaderId: null,
        carId: null,
        plateNum: null,
        sn: null,
        orderFilled: null,
        orderMoney: null,
        orderPrice: null,
        statisticType: null,
        weixinOrderId: null,
        snNumber: null,
        facilitLat: null,
        facilitLng: null,
        facilitProvince: null,
        facilitCity: null,
        facilitArea: null,
        status: null
      },
//地图初始值
      mainForm: {
        address: '',
        lat: '',
        lng: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        facilitId: [
          { required: true, message: "加注机id编号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户账号id编号,司机id不能为空", trigger: "blur" }
        ],
        sn: [
          { required: true, message: "设备唯一标识sn不能为空", trigger: "blur" }
        ],
        orderFilled: [
          { required: true, message: "已加注量(以毫升为单位)不能为空", trigger: "blur" }
        ],
        orderMoney: [
          { required: true, message: "支付总金额(单位:分)不能为空", trigger: "blur" }
        ],
        orderPrice: [
          { required: true, message: "单价(单位:分)不能为空", trigger: "blur" }
        ],
        statisticType: [
          { required: true, message: "订单类型1:公司钱包,2:个人钱包,3:微信不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getFacList();
    this.getCompanyList();
  },
  methods: {
    /** 查询统计总列表 */
    getList() {
      this.loading = true;
      listStatistic(this.queryParams).then(response => {
        this.statisticList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        statisticId: null,
        facilitId: null,
        fillerId: null,
        userId: null,
        userName: null,
        companyId: null,
        mangerId: null,
        leaderId: null,
        carId: null,
        plateNum: null,
        sn: null,
        orderFilled: null,
        orderMoney: null,
        orderPrice: null,
        statisticType: null,
        weixinOrderId: null,
        snNumber: null,
        facilitLat: null,
        facilitLng: null,
        facilitProvince: null,
        facilitCity: null,
        facilitArea: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.statisticId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加统计总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const statisticId = row.statisticId || this.ids
      getStatistic(statisticId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改统计总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.statisticId != null) {
            updateStatistic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStatistic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const statisticIds = row.statisticId || this.ids;
      this.$modal.confirm('是否确认删除统计总编号为"' + statisticIds + '"的数据项？').then(function() {
        return delStatistic(statisticIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/statistic/export', {
        ...this.queryParams
      }, `statistic_${new Date().getTime()}.xlsx`)
    },

    /**根据经纬度打开地图弹窗*/
    // 打开地图弹窗
    openMap(lat, lng) {
      if (lat === null || lng === null) {
        this.$message.error('经纬度为空');
        return;
      }
      this.$refs.ms.show();
      // 根据经纬度设置初始值
      this.mainForm.lat = lat;
      this.mainForm.lng = lng;
      this.$refs.ms.setLocationByLatLng(this.mainForm.lat, this.mainForm.lng);
      this.GeocoderAddress(this.mainForm.lat, this.mainForm.lng);
    },
    /**根据经纬度打开地图弹窗*/
    GeocoderAddress(lat, lng) {
      let that = this;
      that.geocoder = new qq.maps.Geocoder({
        complete: function (result) {
          var address = result.detail.addressComponents;
          that.form.stationProvince = address.province;
          that.form.stationCity = address.city;
          that.form.stationArea = address.district;
        }
      });
      that.geocoder.getAddress(new qq.maps.LatLng(lat, lng));
      //这里是反的,卧槽
      that.form.stationLatitude = lng;
      that.form.stationLongitude = lat;
    },

    /** 查询加注机列表 */
    getFacList() {
      this.loading = true;
      listFacilit({pageNum: 1,
        pageSize: 10000,}).then(response => {
        this.facilitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 选择加注机查询总量 */
    chooseFac(id) {
      this.queryParams.facilitId = id;
    },

    /** 查询加注机列表 */
    getCompanyList() {
      console.log('getCompanyList');
      this.loading = true;
      listCompany({pageNum: 1,
        pageSize: 10000,}).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log('getCompanyList', response.rows);
      });
    },
    /** 选择公司 */
    chooseCompany(id) {
      this.queryParams.companyId = id;
    }
  }
};
</script>

<style scoped>
.box-card {
  margin-bottom: 10px;
  margin-top: -10px;

}

.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
